package com.lagou.rpc.provider.service;

import com.lagou.rpc.pojo.Server;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Zookeeper业务操作类
 */
@Service
public class ZookeeperService {

    @Autowired
    ZkClient zkClient;

    //服务服务路径
    public static final String serverList = "/server/list/%s";
    public static final String serverPath = "/server/list";

    /**
     * 注册一个服务
     * @param ip
     * @param port
     */
    public void register(String ip,Integer port){
        System.out.println(String.format(serverList,ip+":"+port));
        if(!zkClient.exists(serverPath)){
            zkClient.createPersistent(serverPath,true);
        }
        zkClient.createEphemeralSequential(String.format(serverList,ip+":"+port), new Server(ip,port,null,null));
    }

}
